package com.feimt.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feimt.common.R;
import com.feimt.pojo.Category;
import com.feimt.service.CategoryService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /*新增*/
    @PostMapping
    public R save(@RequestBody Category category){
        log.info("category:{}",category);
        boolean b = categoryService.save(category);
        return b? R.success("新增成功"):R.error("新增失败");
    }

    /*分页查询*/
    @GetMapping("/page")
    public R page(Integer page, Integer pageSize){
        log.info("page={},pageSize={}",page,pageSize);
        /*1. 构建分页*/
        Page<Category> categoryPage = new Page<>(page,pageSize);
        /*2. 构建排序条件*/
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.orderByAsc(Category::getSort);
        qw.orderByDesc(Category::getUpdateTime);
        /*3. 分页查询*/
        categoryService.page(categoryPage,qw);
        /*4 返回数据*/
        return R.success(categoryPage);
    }

    /*修改*/
    @PutMapping
    public R update(@RequestBody Category category){
        log.info("category:{}",category);
        boolean b = categoryService.updateById(category);
        return b?R.success("修改成功"):R.error("修改失败");
    }

    /*删除*/
    @DeleteMapping
    public R deleteById(Long id){
        log.info("id:{}",id);
        /*删除 下面是否有菜   套餐*/
//        categoryService.removeById(id);
        categoryService.remove1(id);

        return R.success("删除成功");
    }

    /*
    * 根据套餐/菜品type  查询所有分类
    * */
    @GetMapping("list")
    public R list(Integer type){
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(type!=null,Category::getType,type);
        List<Category> categoryList = categoryService.list(qw);

        return R.success(categoryList);
    }

}
